package com.cunshe.common;

import com.sunkee.commons.utils.StringUtils;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;

public class FileUtil {

    /**
     * 删除文件
     * @param filePath
     * @return
     */
    public static int deleteFile(String filePath){
        if(StringUtils.isBlank(filePath)){
            return 0;
        }
        if(filePath.contains(WebConsts.UPLOAD_DIRECTORY)){
            filePath = filePath.substring(filePath.indexOf(WebConsts.UPLOAD_DIRECTORY));
        }
        ApplicationHome h = new ApplicationHome(FileUtil.class.getClass());
        File root = h.getDir();
        File newFile = new File(root.getParent() + filePath);
        if(newFile != null && newFile.exists()) {
            newFile.delete();
        }
        return 1;
    }

}
